<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $guarded = ['id'];
    protected $appends = ['user_status','shop_status','status_style','status_arr','total_price_txt','product_num','user_coupon_money_txt']; 

    private $statusTxt = [
        //售后完成
         '-100' => ['user'=>'已退款','shop'=>'已退款','style'=>'warning'], 
        // 已确认之后的取消      
        '-9' => ['user'=>'售后中','shop'=>'售后升级','style'=>'warning'], 
        '-4' => ['user'=>'取消被拒绝','shop'=>'取消被拒绝','style'=>'warning'], 
        '-3' => ['user'=>'取消待确认','shop'=>'申请取消订单','style'=>'warning'],

        '-2' =>['user'=>'不接单','shop'=>'已拒绝', 'style'=>'danger'],
        '-1' => ['user'=>'已取消','shop'=>'已取消','style'=>'warning'],
    	'1' => ['user'=>'待付款','shop'=>'待付款','style'=>'primary'],
    	//线上付款才有用
        '2' => ['user'=>'待接单','shop'=>'待接单','style'=>'info'], 
    	'3' => ['user'=>'待配送','shop'=>'已确认','style'=>'purple'], 
            
        '4' => ['user'=>'待收货','shop'=>'已配送','style'=>'primary'],
       

        '90' => ['user'=>'同意取消订单','shop'=>'同意取消订单','style'=>'success'],

    	'100' => ['user'=>'已完成','shop'=>'已完成','style'=>'success'],
    	'200' => ['user'=>'已评价','shop'=>'已评价','style'=>'success'],
    ];
    
    public function address()
    {
        return $this->belongsTo(UserAddress::class);
    }

    public function userAddress()
    {
        return $this->belongsTo(UserAddress::class,'address_id');
    }

    public function user()
    {
    	return $this->belongsTo(User::class);
    }

    public function agent()
    {
        return $this->belongsTo(User::class);
    }

    public function products()
    {
    	return $this->hasMany(OrderProduct::class);
    }

    public function userCoupon()
    {
        return $this->belongsTo(UserCoupon::class);
    }

    public function getUserStatusAttribute()
    {    	
    	return array_get($this->statusTxt[$this->status],'user');
    }

    public function getShopStatusAttribute()
    {    	
    	return array_get($this->statusTxt[$this->status],'shop');
    }

    public function getStatusStyleAttribute()
    {       
        return array_get($this->statusTxt[$this->status],'style');
    }

    public function getTotalPriceTxtAttribute()
    {       
        return $this->total_price/100;
    }

    public function getTotalAmountTxtAttribute()
    {       
        return $this->total_amount/100;
    }

    public function getUserMoneyTxtAttribute()
    {
        return $this->user_money/100;
    }

    public function getStatusArrAttribute()
    {
        return $this->statusTxt;
    }

    public function getProductNumAttribute()
    {
        return $this->products->count();
    }

    public function getUserCouponMoneyTxtAttribute()
    {
        return $this->user_coupon_money/100;
    }

    public function getProductNameAttribute()
    {
       $product_names = OrderProduct::where('order_id',$this->id)->pluck('product_name')->toArray();
       return implode(',', $product_names);
    }
}
